package com.breeze.blockchain.mybatis.entity;

import static com.breeze.blockchain.common.exception.BizErrorCode.USER_STATUS_LOCK_ERROR;

import cn.hutool.core.date.DatePattern;
import com.breeze.blockchain.common.enums.UserStatusEnum;
import com.breeze.components.mybatis.base.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fhs.core.trans.anno.Trans;
import com.fhs.core.trans.constant.TransType;
import com.fhs.core.trans.vo.TransPojo;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Table;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 管理员表 实体类。
 *
 * @author breeze
 * @since 2024-05-11 22:55:04
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Schema(title = "管理员表")
@Table("sys_admin")
public class AdminDo extends BaseEntity implements Serializable, TransPojo {

  /** 昵称 */
  @Schema(title = "昵称")
  private String nickname;

  /** 头像 */
  @Schema(title = "头像")
  private String avatar;

  /** 用户名 */
  @Schema(title = "用户名")
  private String username;

  /** 手机号 */
  @Schema(title = "手机号")
  private String mobile;

  /** 密码 */
  @Schema(title = "密码")
  @JsonIgnore
  private String password;

  /** 所属角色ID */
  @Schema(title = "所属角色ID")
  @Trans(type = TransType.SIMPLE, target = RoleDo.class, fields = "name", ref = "roleName")
  private Long roleId;

  /** 账户状态 0-正常 1-停用 2-冻结 */
  @Schema(title = "账户状态 0-正常 1-停用 2-冻结")
  private Integer status;

  /** 最后登录IP */
  @Schema(title = "最后登录IP")
  private String lastLoginIp;

  /** 最后登录时间 */
  @Schema(title = "最后登录时间")
  @JsonFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
  private LocalDateTime lastLoginTime;

  @Column(ignore = true)
  @Schema(title = "角色名称")
  private String roleName;

  @Column(ignore = true)
  @Schema(title = "token")
  private String accessToken;

  public void checkStatus() {
    USER_STATUS_LOCK_ERROR.equals(UserStatusEnum.NORMAL.getCode(), this.getStatus());
  }
}
